/**
 * Copyright (C), 2020-2021 by colley.ma File Name: BoundContext.java Encoding: UTF-8 Date:
 * 2021年3月30日 下午2:04:06
 * History:
 */
package com.swak.frame.context;

import java.lang.reflect.Method;
import java.util.Map;

import com.swak.frame.dto.base.Command;
import com.swak.frame.dto.request.SwakRequest;
import com.swak.frame.filter.annotation.SwakMethod;
import com.swak.frame.ratelimit.annotation.RateLimit;

public class BoundContext extends Command {
    private static final long serialVersionUID = -8313792960262487455L;

    /** 请求参数 **/
    private SwakRequest request;

    /** 应用ID **/
    private String appId;

    /** 渠道Token **/
    private String token;

    /** 渠道ID **/
    private Integer channelId;

    private Method method;

    private Class<?> targetClass;

    /** 鉴权码 服务唯一 **/
    private String authCode;


    /** 额外属性 **/
    private Map<String, String> ext;

    /** API注解 **/
    private SwakMethod swakMethod;

    /** 限流控制 **/
    private RateLimit rateLimit;

    /** 校验是否登录 **/
    private boolean requiredLogin;

    public BoundContext() {}

    public BoundContext(SwakRequest request, SwakMethod swakMethod, String authCode,
        RateLimit rateLimit) {
        this.request = request;
        this.authCode = authCode;
        this.requiredLogin = swakMethod.requiredLogin();
        this.rateLimit = rateLimit;// 是否开启限流
        this.swakMethod = swakMethod;
    }

    public BoundContext(String authCode, SwakRequest request) {
        this.authCode = authCode;
        this.request = request;
    }

    public SwakRequest getRequest() {
        return request;
    }

    public void setRequest(SwakRequest request) {
        this.request = request;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Integer getChannelId() {
        return channelId;
    }

    public void setChannelId(Integer channelId) {
        this.channelId = channelId;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

    public Map<String, String> getExt() {
        return ext;
    }

    public void setExt(Map<String, String> ext) {
        this.ext = ext;
    }

    public SwakMethod getSwakMethod() {
        return swakMethod;
    }

    public void setSwakMethod(SwakMethod swakMethod) {
        this.swakMethod = swakMethod;
    }

    public RateLimit getRateLimit() {
        return rateLimit;
    }

    public void setRateLimit(RateLimit rateLimit) {
        this.rateLimit = rateLimit;
    }

    public boolean isRequiredLogin() {
        return requiredLogin;
    }

    public void setRequiredLogin(boolean requiredLogin) {
        this.requiredLogin = requiredLogin;
    }

    public Class<?> getTargetClass() {
        return targetClass;
    }

    public void setTargetClass(Class<?> targetClass) {
        this.targetClass = targetClass;
    }

    public static class Builder {
        private SwakRequest request;
        private String appId;
        private String token;
        private Integer channelId;
        private Method method;
        private Class<?> targetClass;
        private String authCode;
        private Map<String, String> ext;
        private SwakMethod swakMethod;
        private RateLimit rateLimit;
        private boolean requiredLogin;

        public Builder request(SwakRequest request) {
            this.request = request;
            return this;
        }

        public Builder appId(String appId) {
            this.appId = appId;
            return this;
        }

        public Builder token(String token) {
            this.token = token;
            return this;
        }

        public Builder channelId(Integer channelId) {
            this.channelId = channelId;
            return this;
        }

        public Builder method(Method method) {
            this.method = method;
            return this;
        }

        public Builder targetClass(Class<?> targetClass) {
            this.targetClass = targetClass;
            return this;
        }

        public Builder authCode(String authCode) {
            this.authCode = authCode;
            return this;
        }

        public Builder ext(Map<String, String> ext) {
            this.ext = ext;
            return this;
        }

        public Builder swakMethod(SwakMethod swakMethod) {
            this.swakMethod = swakMethod;
            return this;
        }

        public Builder rateLimit(RateLimit rateLimit) {
            this.rateLimit = rateLimit;
            return this;
        }

        public Builder requiredLogin(boolean requiredLogin) {
            this.requiredLogin = requiredLogin;
            return this;
        }

        public BoundContext build() {
            return new BoundContext(this);
        }
    }

    private BoundContext(Builder builder) {
        this.request = builder.request;
        this.appId = builder.appId;
        this.token = builder.token;
        this.channelId = builder.channelId;
        this.method = builder.method;
        this.targetClass = builder.targetClass;
        this.authCode = builder.authCode;
        this.ext = builder.ext;
        this.swakMethod = builder.swakMethod;
        this.rateLimit = builder.rateLimit;
        this.requiredLogin = builder.requiredLogin;
    }
}
